package com.neusoft.adminweb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.adminweb.constant.WebConstant;
import com.neusoft.adminweb.entity.Admin;
import com.neusoft.adminweb.entity.Dorm;
import com.neusoft.adminweb.service.AdminService;
import com.neusoft.adminweb.service.DormService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * (Admin)表控制层
 *
 * @author makejava
 * @since 2020-11-09 10:29:10
 */
@Controller
@RequestMapping("admin")
public class AdminController implements BaseController<Admin>{
    public static final Logger logger = LoggerFactory.getLogger(AdminController.class);

    /**
     * 服务对象
     */
    @Resource
    private AdminService adminService;
    @Resource
    private DormService dormService;
    private final String FOLDER = "pages/admin/";

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Override
    @ResponseBody
    public Admin queryById(Integer id) {
        return this.adminService.queryById(id);
    }


    @Override
    public String toAddOrEdit(Integer id, Model result) {
        //为添加准备的
        Admin bean = new Admin();
        if (id != null) {
            bean = adminService.queryById(id);
        }
        result.addAttribute("editBean", bean);
        //从数据库中获取所有宿舍楼
        List<Dorm> dormList = dormService.queryPageByArgs(null);
        result.addAttribute("dormList", dormList);
        return FOLDER + "addOrEdit";
    }

    @Override
    public String save(Admin bean, Model result) {
        System.out.println("save");
        if (bean != null) {
            System.out.println("1");
            if (bean.getId() == null) {
                System.out.println("2");
                //添加
                //数据合法性验证
                //用户名是否重复
                bean.setRegDate(new Date());
                boolean insert = adminService.insert(bean);
                if (insert) {
                    result.addAttribute(WebConstant.MSG, "添加失败");
                    return toAddOrEdit(null, result);
                }
            } else {
                //编辑
                boolean update = adminService.update(bean);
                if (update) {
                    result.addAttribute(WebConstant.MSG, "添加失败");
                    return toAddOrEdit(bean.getId(), result);
                }
            }
        }

        return "redirect:/admin/toList";
    }

    @GetMapping("logout")
    public String logout(HttpSession session) {
        //清除session
        session.removeAttribute("loginAdmin");

        return "redirect:/toLogin";
    }

    @Override
    public String toList(Integer pageNum, Integer pageSize, Model result) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Admin> list = adminService.queryPageByArgs(null);
        for(Admin item : list) {
            System.out.println(item.getDorm());
        }
        PageInfo<Admin> pageInfo = new PageInfo<>(list);
        result.addAttribute("pageInfo", pageInfo);

        return FOLDER + "list";
    }

    @Override
    public String search(Admin args, Integer pageNum, Integer pageSize, Model result) {
        System.out.println("search:::args:" + args);
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        System.out.println("search:::pageNum:" + pageNum);
        System.out.println("search:::pageSize:" + pageSize);
        List<Admin> list = adminService.queryPageByArgs(args);
        PageInfo<Admin> pageInfo = new PageInfo<>(list);
        result.addAttribute("pageInfo", pageInfo);

//        return pageInfo;
        return FOLDER + "list";
    }

    @Override
    public String delete(Integer id) {
        if (id != null) {
            adminService.deleteById(id);
        }

        return "redirect:/admin/toList";
    }

    @PostMapping("login")
    public String login(String account, String password, Integer type, String rem, HttpSession session, Model result) {
//        System.out.println("account:" + account + ", password:" + password + ", type:" + type + ", rem:" + rem);
        //验证数据合法性
        if (StringUtils.isEmpty(account)) {
            result.addAttribute(WebConstant.MSG, "账号不能为空");
            return "whites/login";
        }
        if (StringUtils.isEmpty(password)) {
            result.addAttribute(WebConstant.MSG, "密码不能为空");
            return "whites/login";
        }
        if (StringUtils.isEmpty(type)) {
            result.addAttribute(WebConstant.MSG, "请选择用户类型");
            return "whites/login";
        }
        //判断用户类型
        switch (type) {
            case 0://系统管理员
            case 1://宿舍管理员
                Admin bean = adminService.login(account, password, type);
                if (bean == null) {
                    result.addAttribute(WebConstant.MSG, "账号或密码错误");
                    return "whites/login";
                }
                //登录成功
                //是否要记住密码
                //往session保存数据
                session.setAttribute(WebConstant.LOGIN_ADMIN, bean);
                break;
            case 2:
                break;
            default:
                result.addAttribute(WebConstant.MSG, "非法的用户类型");
                return "whites/login";
        }

        return "redirect:/toWelcome";
    }
}